/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package billing;

import com.apple.eawt.Application;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.util.HashMap;

/**
 *
 * @author apple
 */
public class MainFrame extends javax.swing.JFrame {
    
    /**
     * Creates new form MainFrame
     */
    
    AreasModel areasModel;
    CacheDataModel cacheDataModel;
    OrdersModel ordersModel;
    OrdersDetailModel ordersDetailModel;
    ProductModel productModel;
    UserModel userModel;
    
    public MainFrame(){
        initComponents();
        try {
            // set icon
            setIconImage(new ImageIcon("logo.png").getImage());
            Application.getApplication().setDockIconImage(new ImageIcon("logo.png").getImage());
        
            // set frame to center
            this.setLocationRelativeTo(null);
        
            // Load all Model
            areasModel = new AreasModel();
            cacheDataModel = new CacheDataModel();
            ordersModel = new OrdersModel();
            ordersDetailModel = new OrdersDetailModel();
            productModel = new ProductModel();
            userModel = new UserModel();
        
            // load all tables
            Vector allArea = areasModel.getElements();
            Iterator iter = allArea.iterator();
            
            // fetch tables to tab
            while(iter.hasNext()){
                AreasModel row = (AreasModel)iter.next();
                TabForm tab = new TabForm();
                tab.setButtonTitle(row.id);
                formContain.add(row.name,tab);
            }    

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, e.getMessage(),"Error",JOptionPane.WARNING_MESSAGE);
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jSpinner1 = new javax.swing.JSpinner();
        jSlider1 = new javax.swing.JSlider();
        jScrollBar1 = new javax.swing.JScrollBar();
        jScrollPane3 = new javax.swing.JScrollPane();
        formContain = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        menuAddArea = new javax.swing.JMenuItem();
        menuDeleteArea = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        menuAddProduct = new javax.swing.JMenuItem();
        menuDeleteProduct = new javax.swing.JMenuItem();
        menuEditProduct = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        menuViewOrder = new javax.swing.JMenuItem();
        menuDeleteOrder = new javax.swing.JMenuItem();
        menuEditOrder = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setViewportView(jEditorPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tính tiền");
        setBounds(new java.awt.Rectangle(0, 0, 80, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        setSize(new java.awt.Dimension(800, 600));

        formContain.setBounds(new java.awt.Rectangle(0, 0, 800, 600));
        formContain.setMaximumSize(new java.awt.Dimension(800, 600));

        jMenu1.setText("Quản lý");

        jMenu3.setText("Bàn");

        menuAddArea.setText("Thêm");
        menuAddArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAddAreaActionPerformed(evt);
            }
        });
        jMenu3.add(menuAddArea);

        menuDeleteArea.setText("Xóa");
        menuDeleteArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDeleteAreaActionPerformed(evt);
            }
        });
        jMenu3.add(menuDeleteArea);

        jMenu1.add(jMenu3);

        jMenu4.setText("Món");

        menuAddProduct.setText("Thêm");
        menuAddProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAddProductActionPerformed(evt);
            }
        });
        jMenu4.add(menuAddProduct);

        menuDeleteProduct.setText("Xóa");
        jMenu4.add(menuDeleteProduct);

        menuEditProduct.setText("Sửa");
        jMenu4.add(menuEditProduct);

        jMenu1.add(jMenu4);

        jMenu5.setText("Hóa đơn");

        menuViewOrder.setText("Xem");
        jMenu5.add(menuViewOrder);

        menuDeleteOrder.setText("Xóa");
        jMenu5.add(menuDeleteOrder);

        menuEditOrder.setText("Sửa");
        jMenu5.add(menuEditOrder);

        jMenu1.add(jMenu5);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Tùy chỉnh");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(formContain, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(formContain, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuAddProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAddProductActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuAddProductActionPerformed

    private void menuAddAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAddAreaActionPerformed
        try {
            // TODO add your handling code here:
            String areaName = JOptionPane.showInputDialog(this, "Nhập tên bàn: ","Nhập thông tin", 1);
            if(!areaName.equals("") && !areasModel.checkAreaNameExist(areaName)){
                
                HashMap<String,String> areaDataSet = new HashMap<String, String>();
                areaDataSet.put("name", areaName);
                int result = areasModel.insertData(areaDataSet);
                System.out.println(result);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_menuAddAreaActionPerformed

    private void menuDeleteAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuDeleteAreaActionPerformed
        // TODO add your handling code here:
        JOptionPane.showConfirmDialog(null, new FormManageArea(), "Quản lý bàn",JOptionPane.OK_OPTION ,JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_menuDeleteAreaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane formContain;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JMenuItem menuAddArea;
    private javax.swing.JMenuItem menuAddProduct;
    private javax.swing.JMenuItem menuDeleteArea;
    private javax.swing.JMenuItem menuDeleteOrder;
    private javax.swing.JMenuItem menuDeleteProduct;
    private javax.swing.JMenuItem menuEditOrder;
    private javax.swing.JMenuItem menuEditProduct;
    private javax.swing.JMenuItem menuViewOrder;
    // End of variables declaration//GEN-END:variables
}
